Skip to content

fix(idempotency): fix response hook invocation when function returns None #5251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2024

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #5150

Summary

Changes

This PR addresses an issue with the idempotency feature where the response hook is not called when the function returns None, even for idempotent requests.

Things changed:

  • Modified idempotency logic to call the response hook for None returns.
  • Add more functional tests
  • Add more e2e tests

We don't need to change the documentation because this change fixes what should be the expected behavior.

User experience

import datetime
import os
import uuid
from typing import Dict

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.idempotency import (
    DynamoDBPersistenceLayer,
    IdempotencyConfig,
    idempotent_function,
)
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import (
    DataRecord,
)
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger(level="DEBUG")

def my_response_hook(response: Dict, idempotent_data: DataRecord) -> Dict:
    logger.info(f"Idempotent data", extra={"idempotent_data": idempotent_data, "response": response})
    
    # ...

    # Must return the response here
    return response


table = "IdempotencyTable"
dynamodb = DynamoDBPersistenceLayer(table_name=table)
config = IdempotencyConfig(response_hook=my_response_hook)

@idempotent_function(data_keyword_argument="order", config=config, persistence_store=dynamodb)
def process_order(order: dict) -> dict:
    logger.info(f"Processing order id {event.get('order_id')}")

    # return empty response
    return None


def lambda_handler(event: dict, context: LambdaContext):
    config.register_lambda_context(context)  # see Lambda timeouts section
    try:
        return process_order(order=event.get("order"))
    except Exception as err:
        return {"status_code": 400, "error": f"Error processing {str(err)}"}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team September 26, 2024 19:25
@boring-cyborg boring-cyborg bot added the tests label Sep 26, 2024
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Sep 26, 2024
@leandrodamascena leandrodamascena self-assigned this Sep 26, 2024
@github-actions github-actions bot added the bug Something isn't working label Sep 26, 2024
Copy link

codecov bot commented Sep 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.21%. Comparing base (43da947) to head (a3caf68).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5251      +/-   ##
===========================================
- Coverage    96.21%   96.21%   -0.01%     
===========================================
  Files          229      229              
  Lines        10755    10753       -2     
  Branches      2003     2002       -1     
===========================================
- Hits         10348    10346       -2     
  Misses         321      321              
  Partials        86       86              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this, good also that you added new tests.

Copy link

@leandrodamascena leandrodamascena merged commit 72b9546 into develop Sep 27, 2024
11 checks passed
@leandrodamascena leandrodamascena deleted the fix/idempotency-none branch September 27, 2024 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: response hook is not called when idempotent function returns None
2 participants